/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Cliente;
import model.ClienteDao;

/**
 *
 * @author Gustavo
 */
@WebServlet(name = "ClienteController", urlPatterns = {"/ClienteController"})
public class ClienteController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ClienteController</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ClienteController at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            //out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

        Cliente cli = new Cliente();
        ClienteDao cliDao = new ClienteDao();

        cli.setId(request.getParameter("id"));
        cli.setNome(request.getParameter("nome"));
        cli.setSexo(request.getParameter("sexo"));
        cli.setDataNasc(request.getParameter("dataNasc"));
        cli.setTelefone(request.getParameter("telefone"));
        cli.setEmail(request.getParameter("email"));
        cli.setLogin(request.getParameter("login"));
        cli.setSenha(request.getParameter("senha"));
        cli.setRg(request.getParameter("rg"));
        cli.setCpf(request.getParameter("cpf"));

        String acao = request.getParameter("acao");
        String destino = "";

        if (acao.equals("Incluir")) {
            String existe = ClienteDao.clienteDuplicado(cli.getCpf());
//            String existe = ClienteDao.clienteDuplicado("012.345.678-90");
            // Se houve erro, alimenta o atributo msg com o erro
            if (!existe.equalsIgnoreCase("1")) {
                request.setAttribute("msg", existe);
                destino = "erro.jsp";
            } else {
                destino = ClienteDao.Inserir(request, cli);
            }
        } else if (acao.equals("Alterar")) {
            // 
            destino = ClienteDao.Alterar(request, cli);
        } else if (acao.equals("Excluir")) {
            //
            destino = ClienteDao.Excluir(request, cli);
        } else if (acao.equals("Pesquisar")) {
            //
        } else {
            //
        }

        RequestDispatcher rd = request.getRequestDispatcher(destino);
        rd.forward(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

        Cliente cli = new Cliente();
        ClienteDao cliDao = new ClienteDao();

        cli.setId(request.getParameter("id"));
        cli.setNome(request.getParameter("nome"));
        cli.setSexo(request.getParameter("sexo"));
        cli.setDataNasc(request.getParameter("dataNasc"));
        cli.setData(request.getParameter("dataNasc"));
        cli.setTelefone(request.getParameter("telefone"));
        cli.setEmail(request.getParameter("email"));
        cli.setLogin(request.getParameter("login"));
        cli.setSenha(request.getParameter("senha"));
        cli.setRg(request.getParameter("rg"));
        cli.setCpf(request.getParameter("cpf"));

        String acao = request.getParameter("acao");
        String destino = "";

        if (acao.equals("Incluir")) {
            String existe = ClienteDao.clienteDuplicado(cli.getCpf());
            // Se houve erro, alimenta o atributo msg com o erro
            if (existe.equalsIgnoreCase("1")) {
                request.setAttribute("msg", "<script language='javascript'> alert('Cliente já cadastrado');</script>");
                destino = "cadastroCliente.jsp";
            } else {
                destino = ClienteDao.Inserir(request, cli);
            }
        } else if (acao.equals("Alterar")) {
            //
            destino = ClienteDao.Alterar(request, cli);
        } else if (acao.equals("Excluir")) {
            //
            destino = ClienteDao.Excluir(request, cli);
        } else if (acao.equals("Pesquisar")) {
            //
        } else {
            //
        }

        RequestDispatcher rd = request.getRequestDispatcher(destino);
        rd.forward(request, response);

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
